package com.mobile.framework.util;

import java.io.File;
import java.util.Date;

import com.mobile.framework.upload.struts1.UploadFile;
import org.apache.struts.upload.FormFile;



/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-5-4
 * Time: 23:09:32
 * JAVA处理图片不会失真
 */
public class ImageUtil {

    public static String IMAGE_COMMAND = null;
    //Keys.IMAGECOMMAND;

    public static String TEMP_DB_PATH = null;
    //Keys.TEMP_UPLOAD_FILE;

    public static String UPLOAD_FILE = null;
    //Keys.UPLOAD_FILE;

    public static String toJPG(FormFile comLogo,Long memberId , String mobel  ,int width,int height) throws Exception{
        String tempName = ImageUtil.TEMP_DB_PATH + (new java.util.Date()).getTime() + comLogo.getFileName();
        UploadFile.saveFiles(comLogo, tempName);

        //在生成最终图片的路径
        String fileName = (new java.util.Date()).getTime() + ".jpg";
        //String outfilePath = Keys.getUploadAdd(memberId, mobel + "/" + fileName );
        String outfilePath = UPLOAD_FILE + fileName;

        File outfile = new File(outfilePath);
        //生成路径目录树
        outfile.getParentFile().mkdirs();

        //生成新图片
        ImageUtil.toJPG(tempName, outfilePath, width, height);
        return fileName;
    }
    
    public static String toJPG(FormFile comLogo,int width,int height) throws Exception{
        String tempName = ImageUtil.TEMP_DB_PATH + (new java.util.Date()).getTime() + comLogo.getFileName();
        UploadFile.saveFiles(comLogo, tempName);

        //在生成最终图片的路径
        String fileName = (new java.util.Date()).getTime() + ".jpg";
        //String outfilePath = Keys.getUploadAdd(memberId, mobel + "/" + fileName );
        String outfilePath = UPLOAD_FILE + fileName;

        File outfile = new File(outfilePath);
        //生成路径目录树
        outfile.getParentFile().mkdirs();

        //生成新图片
        ImageUtil.toJPG(tempName, outfilePath, width, height);
        return fileName;
    }
    
    //按比例
    public static String toJPGByScale(FormFile comLogo,int maxWidth,int maxHeight) throws Exception{
    	
    	
    	String tempName = ImageUtil.TEMP_DB_PATH + (new java.util.Date()).getTime() + comLogo.getFileName();
    	//生成一个临时文件
    	
    	UploadFile.saveFiles(comLogo, tempName);
    	
    	java.io.File   file   =   new   java.io.File(tempName);     
        java.awt.image.BufferedImage   bi   =   javax.imageio.ImageIO.read(file);   
        int width = bi.getWidth();
        int height = bi.getHeight();
        
        String fileName = (new java.util.Date()).getTime() + ".jpg";
        String outfilePath = UPLOAD_FILE + fileName;
        double[] src = ImageUtil.getWidthHeight(width, height, maxWidth, maxHeight);
        File outfile = new File(outfilePath);
        outfile.getParentFile().mkdirs();
        
        ImageUtil.toJPG(tempName, outfilePath,(int)src[0],(int)src[1]);
        
        return fileName;
   
    }
    
    public static String toJPG(FormFile comLogo) throws Exception{
        String tempName = ImageUtil.TEMP_DB_PATH + (new java.util.Date()).getTime() ;
        UploadFile.saveFiles(comLogo, tempName);

        //在生成最终图片的路径
        String fileName = (new java.util.Date()).getTime() + ".jpg";
        //String outfilePath = Keys.getUploadAdd(memberId, mobel + "/" + fileName );
        String outfilePath = UPLOAD_FILE + fileName;

        File outfile = new File(outfilePath);
        //生成路径目录树
        outfile.getParentFile().mkdirs();

        //生成新图片
        ImageUtil.toJPG(tempName, outfilePath);
        return fileName;
    }

    /*
     * 比例计算宽度和高度
     * @param srcWidth  原图片的宽
     * @param srcHeight 原图片的高
     * @param maxWidth  最大的宽
     * @param maxHeight 最大的高
     */
    public static double[] getWidthHeight(double srcWidth, double srcHeight, double maxWidth, double maxHeight) {

        if (srcWidth > srcHeight) {
            if (srcWidth > maxWidth) {
                srcHeight = maxWidth / srcWidth * srcHeight;
                srcWidth = maxWidth;
            }
            if (srcHeight > maxHeight) {
                srcWidth = maxHeight / srcHeight * srcWidth;
                srcHeight = maxHeight;
            }
        } else {
            if (srcHeight > maxHeight) {
                srcWidth = maxHeight / srcHeight * srcWidth;
                srcHeight = maxHeight;
            }
            if (srcWidth > maxWidth) {
                srcHeight = maxWidth / srcWidth * srcHeight;
                srcWidth = maxWidth;
            }
        }
        return new double[]{srcWidth, srcHeight};
    }

    /**
     *
     * @param src           源文件
     * @param outfile       目标文件
     * @param drawX         截取的X坐标
     * @param drawY         截取的y坐标
     * @param drawWidth     截取x长度
     * @param drawHeight    截取y长度
     * @param wWidth        输出图片宽度
     * @param wHeight       输出图片高度
     * @throws Exception
     */
    public static void toJPG(String src,String outfile,int drawX, int drawY, int drawWidth, int drawHeight,int wWidth, int wHeight)  throws Exception {
    		//生成一张临时图片
        String tempImage = ImageUtil.TEMP_DB_PATH + new Date().getTime() + ".jpg";
        //按照x,y,width,height切割图片
        String cmd = ImageUtil.IMAGE_COMMAND  + " -crop " + drawX + "x" + drawY + "+" + drawWidth  + "+" + drawHeight + " " + src + " " + tempImage;
        //命令原型
        //convert -crop 100x200+100+200 c:\1.jpg c:\2.jpg
        Process process = Runtime.getRuntime().exec(cmd);
        process.waitFor();
        //通过切割得到新图片,再按照新的宽度和高度生成新图片
        ImageUtil.toJPG(tempImage,outfile,wWidth,wHeight);
        try{
        	//删除临时文件
        	File tempFile = new File(tempImage);
        	tempFile.delete();
      	}catch(Exception ex){
      		
      	}
    }

    /**
     * 改变大小输出jpg文件,
     * @param source        源文件
     * @param outfile       目标文件
     * @param width         输出图片宽度
     * @param height        输出图片高度
     * @throws Exception
     */
    public static void toJPG(String source, String outfile, int width, int height) throws Exception {
        String cmd = ImageUtil.IMAGE_COMMAND  + " " + source + " -resize " + width + "x" + height + "!" + " " + outfile;
        //命令原型
        //convert c:\1.jpg -resize "100x100!" c:\2.jpg
        System.out.println(cmd);
        Process process = Runtime.getRuntime().exec(cmd);
        process.waitFor();
    }

    /**
     * 转换格式
     * @param source    源文件
     * @param outfile   目标文件
     * @throws Exception
     */
    public static void toJPG(String source, String outfile)  throws Exception {
        String cmd = ImageUtil.IMAGE_COMMAND  + " " + source + " " + outfile;
        //命令原型
        //convert c:\1.jpg c:\2.jpg
        System.out.println(cmd);
        Process process =  Runtime.getRuntime().exec(ImageUtil.IMAGE_COMMAND  + " " + source + " " + outfile);
        process.waitFor();
    }

    public static void main(String[] arge) {
       // ImageUtil.toJPG("G:/hodv20546.jpg", "g:/1.jpg", 0, 0, 100, 100);
    }

}
